/**
 * Copyright &copy; 2015-2020 <a href="http://www.jeeplus.org/">JeePlus</a> All rights reserved.
 */
package com.jeeplus.modules.web.web;

import com.google.common.collect.Lists;
import com.jeeplus.common.config.Global;
import com.jeeplus.common.json.AjaxJson;
import com.jeeplus.common.utils.DateUtils;
import com.jeeplus.common.utils.StringUtils;
import com.jeeplus.common.utils.excel.ExportExcel;
import com.jeeplus.common.utils.excel.ImportExcel;
import com.jeeplus.core.persistence.Page;
import com.jeeplus.core.web.BaseController;
import com.jeeplus.modules.web.entity.WxUserWalletDetails;
import com.jeeplus.modules.web.service.WxUserWalletDetailsService;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.ConstraintViolationException;
import java.util.List;
import java.util.Map;

/**
 * 钱包Controller
 * @author rongjie
 * @version 2017-08-24
 */
@Controller
@RequestMapping(value = "${adminPath}/web/wxUserWalletDetails")
public class WxUserWalletDetailsController extends BaseController {

	@Autowired
	private WxUserWalletDetailsService wxUserWalletDetailsService;

	@ModelAttribute
	public WxUserWalletDetails get(@RequestParam(required=false) String id) {
		WxUserWalletDetails entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = wxUserWalletDetailsService.get(id);
		}
		if (entity == null){
			entity = new WxUserWalletDetails();
		}
		return entity;
	}

	/**
	 * 钱包列表页面
	 */
	@RequiresPermissions("web:wxUserWalletDetails:list")
	@RequestMapping(value = {"list", ""})
	public String list() {
		return "modules/web/wxUserWalletDetailsList";
	}

	/**
	 * 钱包列表数据
	 */
	@ResponseBody
	@RequiresPermissions("web:wxUserWalletDetails:list")
	@RequestMapping(value = "data")
	public Map<String, Object> data(WxUserWalletDetails wxUserWalletDetails, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<WxUserWalletDetails> page = wxUserWalletDetailsService.findPage(new Page<WxUserWalletDetails>(request, response), wxUserWalletDetails);
		return getBootstrapData(page);
	}

	/**
	 * 导出excel文件
	 */
	@ResponseBody
	@RequiresPermissions("web:wxUserWalletDetails:export")
    @RequestMapping(value = "export", method=RequestMethod.POST)
    public AjaxJson exportFile(WxUserWalletDetails wxUserWalletDetails, HttpServletRequest request, HttpServletResponse response, RedirectAttributes redirectAttributes) {
		AjaxJson j = new AjaxJson();
		try {
            String fileName = "钱包"+DateUtils.getDate("yyyyMMddHHmmss")+".xlsx";
            Page<WxUserWalletDetails> page = wxUserWalletDetailsService.findPage(new Page<WxUserWalletDetails>(request, response, -1), wxUserWalletDetails);
    		new ExportExcel("钱包", WxUserWalletDetails.class).setDataList(page.getList()).write(response, fileName).dispose();
    		j.setSuccess(true);
    		j.setMsg("导出成功！");
    		return j;
		} catch (Exception e) {
			j.setSuccess(false);
			j.setMsg("导出钱包记录失败！失败信息："+e.getMessage());
		}
			return j;
    }

}
